﻿#if UNITY_IOS
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using GMME.Model.Rtm.Result;
using GMME.Model.Rtm.Notify;
using UnityEngine;

namespace GMME
{
    public class CallbackHandlerForIos : CallbackHandlerForEditor
    {
        public void onMuteAllPlayers(string roomId, string openIds, bool isMuted, int code, string msg)
        {
            List<string> openIdList = new List<string>(openIds.Split(','));
            Debug.LogFormat("handler onMutePlayer.roomId ={0}, isMuted={1}, code={2}, msg={3}",
                roomId, isMuted, code, msg);
            onMuteAllPlayers(roomId, openIdList, isMuted, code, msg);
        }

        public void onSpeakersDetectionEx(string userVolumeInfos)
        {
            List<VolumeInfo> list = JsonUtil.FromJson<List<VolumeInfo>>(userVolumeInfos);
            Debug.LogFormat("handler onSpeakersDetectionEx.userVolumeInfos size ={0} ", list.Count);
            List<string> openIdList = new List<string>(list.Count);
            foreach (var volumeInfo in list)
            {
                openIdList.Add(volumeInfo.OpenId);
            }

            onSpeakersDetectionEx(list, openIdList);
        }

        public void onForbidAllPlayers(string roomId, string openIds, bool isForbidden, int code, string msg)
        {
            List<string> openIdList = new List<string>(openIds.Split(','));
            Debug.LogFormat("handler onForbidAllPlayers.roomId ={0}, isForbidden={1}, code={2}, msg={3} ",
                roomId, isForbidden, code, msg);
            onForbidAllPlayers(roomId, openIdList, isForbidden, code, msg);
        }

        public void onForbiddenByOwner(string roomId, string openIds, bool isForbidden)
        {
            List<string> openIdList = new List<string>(openIds.Split(','));
            Debug.LogFormat("handler onForbiddenByOwner.roomId ={0}, openIds size ={1}, isForbidden={2} ", roomId,
                openIdList.Count, isForbidden);
            onForbiddenByOwnerComplete(roomId, openIdList, isForbidden);
        }

        public void onAudioClipStateChangedNotify(string audioPlayStateInfoStr)
        {
            Debug.LogFormat("handler onAudioClipStateChangedNotify. the value is : {0}", audioPlayStateInfoStr);
            AudioPlayStateInfo audioPlayStateInfo = JsonUtil.FromJson<AudioPlayStateInfo>(audioPlayStateInfoStr);
            audioPlayStateInfo.StateEnum =
                (HWAudioClipsStateEnum) Enum.ToObject(typeof(HWAudioClipsStateEnum), audioPlayStateInfo.State);
            onAudioClipStateChangedNotify(audioPlayStateInfo);
        }


        #region RTM

        /**
         * 订阅RTM频道回调
         *
         * @param result 订阅RTM频道结果
         */
        public void onSubscribeRtmChannel(string result)
        {
            Debug.LogFormat("handler onSubscribeRtmChannel");
            SubscribeRtmChannelResult subscribeRtmChannelResult = JsonUtil.FromJson<SubscribeRtmChannelResult>(result);
            onSubscribeRtmChannel(subscribeRtmChannelResult);
        }

        /**
         * 取消订阅RTM频道回调
         *
         * @param result 取消订阅RTM频道结果
         */
        public void onUnSubscribeRtmChannel(string result)
        {
            Debug.LogFormat("handler onUnSubscribeRtmChannel.");
            UnSubscribeRtmChannelResult unSubscribeRtmChannelResult =
                JsonUtil.FromJson<UnSubscribeRtmChannelResult>(result);
            onUnSubscribeRtmChannel(unSubscribeRtmChannelResult);
        }

        /**
         * 发布RTM频道消息回调
         *
         * @param result 发布RTM频道消息结果
         */
        public void onPublishRtmChannelMessage(string result)
        {
            Debug.LogFormat("handler onPublishRtmChannelMessage.");
            PublishRtmChannelMessageResult publishRtmChannelMessageResult =
                JsonUtil.FromJson<PublishRtmChannelMessageResult>(result);
            onPublishRtmChannelMessage(publishRtmChannelMessageResult);
        }

        /**
         * 发布RTM点对点消息回调
         *
         * @param result 发布RTM点对点消息结果
         */
        public void onPublishRtmPeerMessage(string result)
        {
            Debug.LogFormat("handler onPublishRtmPeerMessage.");
            PublishRtmPeerMessageResult peerMessageResult =
                JsonUtil.FromJson<PublishRtmPeerMessageResult>(result);
            onPublishRtmPeerMessage(peerMessageResult);
        }

        /**
         * 获取RTM频道信息回调
         *
         * @param result 获取RTM频道信息结果
         */
        public void onGetRtmChannelInfo(string result)
        {
            Debug.LogFormat("handler onGetRtmChannelInfo. ");
            GetRtmChannelInfoResult getRtmChannelInfoResult =
                JsonUtil.FromJson<GetRtmChannelInfoResult>(result);
            onGetRtmChannelInfo(getRtmChannelInfoResult);
        }

        /**
         * 接收RTM频道信息通知
         *
         * @param notify 接收RTM频道信息结果
         */
        public void onReceiveRtmChannelMessage(string notify)
        {
            Debug.LogFormat("handler onReceiveRtmChannelMessage.");
            ReceiveRtmChannelMessageNotify rtmChannelMessageNotify =
                JsonUtil.FromJson<ReceiveRtmChannelMessageNotify>(notify);
            onReceiveRtmChannelMessage(rtmChannelMessageNotify);
        }

        /**
         * 接收RTM点对点信息通知
         *
         * @param notify 接收RTM点对点信息结果
         */
        public void onReceiveRtmPeerMessage(string notify)
        {
            Debug.LogFormat("handler onReceiveRtmPeerMessage.");
            ReceiveRtmPeerMessageNotify receiveRtmPeerMessageNotify =
                JsonUtil.FromJson<ReceiveRtmPeerMessageNotify>(notify);
            onReceiveRtmPeerMessage(receiveRtmPeerMessageNotify);
        }

        /**
         * RTM连接状态通知
         *
         * @param notify RTM连接状态结果
         */
        public void onRtmConnectionChanged(string notify)
        {
            Debug.LogFormat("handler onRtmConnectionChanged.");

            RtmConnectionStatusNotify rtmConnectionStatusNotify =
                JsonUtil.FromJson<RtmConnectionStatusNotify>(notify);
            onRtmConnectionChanged(rtmConnectionStatusNotify);
        }

        /**
         * 设置频道内玩家属性结果回调
         *
         * @param result 设置频道内玩家属性结果
         */
        public void onSetRtmChannelPlayerProperties(string result)
        {
            Debug.LogFormat("handler onSetRtmChannelPlayerProperties.");
            SetRtmChannelPlayerPropertiesResult setRtmChannelPlayerPropertiesResult =
                JsonUtil.FromJson<SetRtmChannelPlayerPropertiesResult>(result);

            onSetRtmChannelPlayerProperties(setRtmChannelPlayerPropertiesResult);
        }

        /**
         * 查询频道内玩家属性结果回调
         *
         * @param result 查询频道内玩家属性结果
         */
        public void onGetRtmChannelPlayerProperties(string result)
        {
            Debug.LogFormat("handler onGetRtmChannelPlayerProperties.");
            GetRtmChannelPlayerPropertiesResult getRtmChannelPlayerPropertiesResult =
                JsonUtil.FromJson<GetRtmChannelPlayerPropertiesResult>(result);
            getRtmChannelPlayerPropertiesResult.SetPlayerProperties();
            onGetRtmChannelPlayerProperties(getRtmChannelPlayerPropertiesResult);
        }

        /**
         * 删除频道内玩家属性结果回调
         *
         * @param result 删除频道内玩家属性结果
         */
        public void onDeleteRtmChannelPlayerProperties(string result)
        {
            Debug.LogFormat("handler onDeleteRtmChannelPlayerProperties.");
            DeleteRtmChannelPlayerPropertiesResult deleteRtmChannelPlayerPropertiesResult =
                JsonUtil.FromJson<DeleteRtmChannelPlayerPropertiesResult>(result);
            onDeleteRtmChannelPlayerProperties(deleteRtmChannelPlayerPropertiesResult);
        }

        /**
         * 设置频道属性结果回调
         *
         * @param result 设置频道属性结果
         */
        public void onSetRtmChannelProperties(string result)
        {
            Debug.LogFormat("handler onSetRtmChannelProperties.");
            SetRtmChannelPropertiesResult setRtmChannelPropertiesResult =
                JsonUtil.FromJson<SetRtmChannelPropertiesResult>(result);
            onSetRtmChannelProperties(setRtmChannelPropertiesResult);
        }

        /**
         * 查询频道属性结果回调
         *
         * @param result 查询频道属性结果
         */
        public void onGetRtmChannelProperties(string result)
        {
            Debug.LogFormat("handler onGetRtmChannelProperties.");
            GetRtmChannelPropertiesResult getRtmChannelPropertiesResult =
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               JsonUtil.FromJson<GetRtmChannelPropertiesResult>(result);
            getRtmChannelPropertiesResult.ConvertPropertiesToDict();
            onGetRtmChannelProperties(getRtmChannelPropertiesResult);
        }

        /**
         * 删除频道属性结果回调
         *
         * @param result 删除频道属性结果
         */
        public void onDeleteRtmChannelProperties(string result)
        {
            Debug.LogFormat("handler onDeleteRtmChannelProperties.");
            DeleteRtmChannelPropertiesResult deleteRtmChannelPropertiesResult =
                JsonUtil.FromJson<DeleteRtmChannelPropertiesResult>(result);
            onDeleteRtmChannelProperties(deleteRtmChannelPropertiesResult);
        }

        /**
         * 查询频道历史消息回调
         *
         * @param result 查询频道历史消息结果
         */
        public void onGetRtmChannelHistoryMessages(string result)
        {
            Debug.LogFormat("handler onGetRtmChannelHistoryMessages.");
            GetRtmChannelHistoryMessagesResult getRtmChannelHistoryMessagesResult =
                JsonUtil.FromJson<GetRtmChannelHistoryMessagesResult>(result);
            onGetRtmChannelHistoryMessages(getRtmChannelHistoryMessagesResult);
        }

        /**
         * 频道内玩家属性变更通知
         *
         * @param notify 频道内玩家属性变更
         */
        public void onRtmChannelPlayerPropertiesChanged(string notify)
        {
            Debug.LogFormat("handler onRtmChannelPlayerPropertiesChanged.");
            RtmChannelPlayerPropertiesNotify rtmChannelPlayerPropertiesNotify =
                JsonUtil.FromJson<RtmChannelPlayerPropertiesNotify>(notify);
            rtmChannelPlayerPropertiesNotify.SetPlayerProperties();
            onRtmChannelPlayerPropertiesChanged(rtmChannelPlayerPropertiesNotify);
        }

        /**
         * 频道属性变更通知
         *
         * @param notify 频道属性变更
         */
        public void onRtmChannelPropertiesChanged(string notify)
        {
            Debug.LogFormat("handler onRtmChannelPropertiesChanged. ");
            RtmChannelPropertiesNotify rtmChannelPropertiesNotify =
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                JsonUtil.FromJson<RtmChannelPropertiesNotify>(notify);
            rtmChannelPropertiesNotify.ConvertPropertiesToDict();
            onRtmChannelPropertiesChanged(rtmChannelPropertiesNotify);
        }

        #endregion

    }
}
#endif